package com.zhongjian.technologycal.mall.net;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.SparseArray;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zhongjian.technologycal.mall.app.MyApplication;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.http.CaheInterceptor;
import com.zhongjian.technologycal.mall.http.MyApiService;
import com.zhongjian.technologycal.mall.http.NovateCookieManger;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wei on 2017/11/30.
 */

public class ApiRetrofitClient {
    private static final int DEFAULT_TIMEOUT = 20;
    private MyApiService apiService;
    private static OkHttpClient okHttpClient;
    private static Context mContext;
    private static ApiRetrofitClient sNewInstance;

    private static Retrofit retrofit;
    private Cache cache = null;
    private File httpCacheDirectory;

    public static ApiRetrofitClient getInstance(Context context) {
        if (context != null) {
            mContext = context;
        }

        return new ApiRetrofitClient(context);
    }


    private ApiRetrofitClient(Context context) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(
                        new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .cookieJar(new NovateCookieManger(context))
                .addNetworkInterceptor(new CaheInterceptor(context))
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool(8, 15, TimeUnit.SECONDS))
                // 这里你可以根据自己的机型设置同时连接的个数和时间，我这里8个，和每个保持时间为15s
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaseContacts.BASE_REQUEST_UPLOAD_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create())
                .build();
        apiService = retrofit.create(MyApiService.class);

    }

    //更新头像
    public Subscription postUrlRequestUploading(String token, String headPhoto,Subscriber<ResponseBody> subscriber) {
        return apiService.postDefaultUrlUpload(token,headPhoto)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber);
    }

    //上传图片
    public Subscription postUrlRequestUploadPhoto(MultipartBody.Part file, String token,Subscriber<ResponseBody> subscriber) {
        return apiService.postDefaultUrlUploadPhoto(file,token)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber);
    }

    //检查版本号
    public Subscription getUrlRequestVersion( String appCurrentVersion,Subscriber<ResponseBody> subscriber) {
        return apiService.getDefaultUrlVersion(appCurrentVersion)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber);
    }

    //检查是否有红包
    public Subscription getUrlRequestGetBeginningRedPacketKey(String token, Subscriber<ResponseBody> subscriber) {
        return apiService.getDefaultUrlOGetBeginningRedPacketKey(token)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber);
    }
    //领取红包
    public Subscription postUrlRequestdrawRedPacket(String token,String redPacketKey, Subscriber<ResponseBody> subscriber) {
        return apiService.postDefaultUrldrawRedPacket(token,redPacketKey)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber);
    }


}
